home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15102 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: mayne.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: how to prevent overflow in unsigned?
  5. Date: 16 Apr 1996 13:44:01 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4l10qhINN8gu@mayne.ugrad.cs.ubc.ca>
  8. References: <4l0i9b$efq@gwdu19.gwdg.de>
  9. NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
  10.  
  11. In article <4l0i9b$efq@gwdu19.gwdg.de>,
  12. Arne Mueller  <amuelle3@gwdu19.gwdg.de> wrote:
  13.  >Hello,
  14.  >
  15.  >I've the following problem:
  16.  >
  17.  >I have to write a function (I'm not allowed to use the standard functions of stdlib!)
  18.  >that reads an unsigned int from stdin (using getchar). 
  19.  
  20. homework?
  21.  
  22.  >My question: How to recognize an overflowWh 
  23.  >If the number is bigger than UINT_MAX, the variable that saves the number
  24.  >is not set to zero.
  25.  
  26. That's because you aren't setting it to zero.
  27.  
  28.  >some code ... :
  29.  >
  30.  >IO_STATUS unsigned_read(unsigned int *i)
  31.  >{
  32.  >  
  33.  >  int c;
  34.  >  unsigned int x;
  35.  >  unsigned int i_old;
  36.  
  37. Turn on all your compiler warnings. If it's any good, it will warn you that you
  38. are using i_old without initializing it.
  39.  
  40.  >  IO_STATUS stat = IN_NOK;
  41.  > 
  42.  >  i_alt = *i = 0;
  43.  
  44. Ah, since ``alt'' is German for ``old'', I see you are typing code off the top
  45. of your head. No American compiler will recognize the mixup, and permit the
  46. i_old lvalue to be accessed as i_alt.
  47.  
  48.  >  while (isdigit((c = getchar())) != 0)
  49.  >  {
  50.  >    x = (unsigned int) c - 48;    /* 48 is ASCII 0 */
  51.  
  52. Yes, 48 is ascii 0! But why make your code ascii specific, when you can
  53. perfectly well write:  c - '0';  The C language standard guarantees that the
  54. character values for the digits '0'-'9' are consecutive, increasing integers,
  55. so subtracting '0' from a character to obtain the digit value is portable.
  56.  
  57.  >    *i = *i * 10 + x;
  58.  >    
  59.  >    if (*i <= i_old)  
  60.  >    {
  61.  >      puts("too big!"); 
  62.  >      return IN_NOK;
  63.  >    }
  64.  >    i_old = *i; 
  65.  >    stat = IN_OK;
  66.  
  67. You don't need to repeatedly assign to ``stat''. You never use it anyway.
  68.  
  69.  >  }
  70.  > 
  71.  > return OK;
  72.  
  73. I thought it was IN_OK. Which one is it?
  74.  
  75. Repost your program after you have massaged it to the point that it actually
  76. compiles. We can't move on to analyzing the deeper semantics of something that
  77. doesn't even meet simple syntactic or semantic constraints.
  78. -- 
  79. I'm not really a jerk, but I play one on Usenet.
  80.